<template>
  <div class="login-container">
    <div class="login-header">
      <h1 class="login-title"></h1>
    </div>
    <div class="login-box">
      <img src="@/assets/logo.png" alt="Logo" class="logo"/>
      <div class="login-form">
        <input
            type="text"
            v-model="username"
            placeholder="请输入用户名"
            class="login-input"
        />
        <input
            type="password"
            v-model="password"
            placeholder="请输入密码"
            class="login-input"
        />
        <button @click="login" class="login-button">登录</button>
      </div>
    </div>
  </div>
</template>

<script>
import api from "@/api.js"
import dayjs from "dayjs";

export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async login() {
      const response = await api.login(this.username, this.password);
      const data = response.data;
      if (data.code === 200) {
        alert('登录成功');
        const start = new dayjs();
        const end = new dayjs(data.data.expires);
        const expires = end.diff(start, 'day', false);
        api.storeToken(data.data.token, expires);
        localStorage.setItem('role', data.data.role);
        this.$router.push('/main');
      } else {
        alert('登录失败');
        console.log(data.msg);
      }
    },
  },
};
</script>

<style scoped>

body {
  margin: 0;
  padding: 0;
  font-family: "Arial", sans-serif;
  background-color: #fbc2eb;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
}


.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100%;
}

.login-header {
  margin-bottom: -10px; /* 标题与登录框之间的间距 */
}

.login-title {
  font-size: 24px;
  color: #fff;
  text-align: center;
  text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  margin-bottom: 20px; /* 适当的间距 */
}

/* logo的样式 */
.logo {
  width: 180px;
  height: auto;
  display: block;
  margin:  auto;
}

/* 登录框样式 */
.login-box {
  width: 350px;
  padding: 20px 40px;
  background: white;
  border-radius: 15px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
  text-align: center;
}

.login-box h2 {
  font-size: 24px;
  margin-bottom: 20px;
  color: #007bff;
}

/* 输入框的样式 */
.login-input {
  width: 100%;
  padding: 12px;
  margin: 10px 0;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
  outline: none;
  transition: all 0.3s ease;
}

.login-input:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

/* 登录按钮的样式 */
.login-button {
  width: 100%;
  padding: 12px;
  margin-top: 20px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-button:hover {
  background-color: #0056b3;
}

/* 响应式设计，适应不同屏幕 */
@media (max-width: 480px) {
  .login-box {
    width: 90%;
    padding: 20px;
  }

  .login-title {
    font-size: 22px;
  }
}
</style>
